package com.ocl.www.controller;

import com.ocl.www.basic.BasicController;
import com.ocl.www.domain.Mailbox;
import com.ocl.www.domain.User;
import com.ocl.www.service.MailboxService;
import com.ocl.www.service.UserService;
import com.ocl.www.thread.MailboxThread;
import com.ocl.www.utils.UserUntil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/24.
 */

@RestController
@RequestMapping("/mailbox")
public class MailboxController  extends BasicController {
    private Logger log = Logger.getLogger(MailboxController.class);

    @Autowired
    private MailboxService mailboxService;
    @Autowired
    private EhCacheManager em;
    @Autowired
    private UserService userService;
    @Autowired
    private MailboxThread mailboxThread;



    private Map<String,Object>  map = new HashedMap();
    /**
     * description： 添加一条私信
     * @param maptemp
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/sendLetter", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map<String,Object> sendLetter(@RequestBody Map<String,Object> maptemp){
        try {

            User user = new User();
            user.setUserId(UserUntil.getUserId(SecurityUtils.getSubject().getPrincipals().asSet()));
            user = userService.findUserInfo2(user);

            Mailbox mailbox = new Mailbox();
            mailbox.setRecipientId(user.getUserId());
            mailbox.setRecipienName(user.getNickName());
            mailbox.setSenderId((Integer) maptemp.get("senderId"));
            mailbox.setSenderName((String) maptemp.get("senderName"));
            mailbox.setLetterContent((String) maptemp.get("letterContent"));

            int c = mailboxService.insertMailbox(mailbox);
            if (c==1){
                map.put("info","1");
                map.put("tip","添加成功！");

                Thread thread = new Thread(mailboxThread);
                thread.start();
                log.info("此时线程还没有执行完才是正确的！");
            }
        } catch (Exception e) {
            map.put("info","0");
            map.put("tip",e.getMessage());
        }
        return map;
    }


    /**
     * description： 查询某个用户的私信
     * @param maptemp
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectLettersOfUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map<String,Object> selectLettersOfUser(@RequestBody Map<String,Object> maptemp){

        try {
            User user = new User();
            user.setUserId(UserUntil.getUserId(SecurityUtils.getSubject().getPrincipals().asSet()));
            Mailbox mailbox = new Mailbox();
            mailbox.setSenderId(user.getUserId());
            mailbox.setRecipientId(user.getUserId());
            map.put("info","1");
            map.put("mailboxList",mailboxService.selectLettersOfUser(mailbox));


        } catch (Exception e) {

            map.put("info","0");
            map.put("tip",e.getMessage());
        }

        return map;

    }
}
